package com.chenlonghai.sbmybatis.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.chenlonghai.sbmybatis.entity.AppModelTest;

import net.sf.json.JSONObject;;

@RestController
@RequestMapping("/app")
public class AppTestController {
	@Value("${cbs.location}")
    private String locationPath;
	
	
	@RequestMapping(value="/order/detail",method = RequestMethod.POST)
	public Map<String, Object> getorderdetail(String merchantId){
		 Map<String, Object>modelMap = new HashMap<>();
		 AppModelTest<String>result = new AppModelTest<String>("获得内容",modelMap);
		 System.out.println(merchantId);
		 return  result.getResultMap();
	}
	
	
	@RequestMapping(value="/uploadFile",method = RequestMethod.POST)
	public Map<String, Object> getFile(MultipartFile file,
            HttpServletRequest request){
		
		  Map<String, Object>modelMap = new HashMap<>();
	     if (!file.isEmpty()) {

	 		    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
	            String saveFileName = df.format(new Date()) + file.getOriginalFilename();
	            
	          
	            System.out.println("1->"+saveFileName);
	            File saveFile = new File(locationPath + saveFileName);
	            if (!saveFile.getParentFile().exists()) {
	                saveFile.getParentFile().mkdirs();
	            }
	            try {
	                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
	                out.write(file.getBytes());
	                out.flush();
	                out.close();
	                modelMap.put("path", "/images/"+saveFileName);
	           
             	 AppModelTest<String>result = new AppModelTest<String>("1",modelMap);
                  return result.getResultMap();
	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	           	 AppModelTest<String>result = new AppModelTest<String>(null,modelMap);
                 return result.getResultMap();
	            } catch (IOException e) {
	                e.printStackTrace();
	              	 AppModelTest<String>result = new AppModelTest<String>(null,modelMap);
	                 return result.getResultMap();
	            }
	        } else {
	          	 AppModelTest<String>result = new AppModelTest<String>(null,modelMap);
                 return result.getResultMap();

	    }
		
	}
	
}
